package com.deeper.customview.ui.c5.demo.c05d01;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.deeper.customview.R;

public class FirstView extends View {
    private Paint paint;
    private String text;


    public FirstView(Context context) {
        super(context);
    }


    public FirstView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        //读取属性值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FirstView);
        text = typedArray.getString(R.styleable.FirstView_text);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(100);
        paint.setColor(Color.RED);

        typedArray.recycle();
    }


    public FirstView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //将文字放在中间
        Rect textRect = this.getTextRect();
        int viewWidth = getMeasuredWidth();
        int viewHeight = getMeasuredHeight();

        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        int x = (viewWidth - textRect.width()) / 2;
        int y = (int) ((viewHeight / 2 + (fontMetrics.descent - fontMetrics.ascent) / 2) - fontMetrics.descent);
        canvas.drawText(text, x, y, paint);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Rect rect = getTextRect();
        int textWidth = rect.width();
        int textHeight = rect.height();
        int width = measureWidth(widthMeasureSpec, textWidth);
        int height = measureHeight(heightMeasureSpec, textHeight);
        setMeasuredDimension(width, height);
    }


    /**
     * 获取文字所占的尺寸
     */
    private Rect getTextRect() {
        //根据Paint设置的绘图参数计算文字所占宽度
        Rect rect = new Rect();
        //文字所占区域大小保存在rect中
        paint.getTextBounds(text, 0, text.length(), rect);
        return rect;
    }


    /**
     * 测量组件宽度
     */
    private int measureWidth(int widthMeasureSpec, int textWidth) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int width = 0;
        if (mode == MeasureSpec.EXACTLY) {
            width = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            width = textWidth;
        }

        return width;
    }


    /**
     * 测量组件高度
     */
    private int measureHeight(int heightMeasureSpec, int textHeight) {
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);
        int height = 0;
        if (mode == MeasureSpec.EXACTLY) {
            height = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            height = textHeight;
        }

        return height;
    }
}
